/**
 * @file conn_writer.h
 * @author HaoyuWang (136046355@qq.com)
 * @brief 自定义socket写缓冲区，也可以通过setsockopt直接修改写缓冲区大小
 * @version 0.1
 * @date 2025-02-11
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#ifndef __CONN_WRITER_H__
#define __CONN_WRITER_H__


#include <list>
#include <stdint.h>
#include <memory>


class WriteObject {
public:
    std::streamsize start_;
    std::streamsize len_;
    std::shared_ptr<char> buff_;
};


class ConnWriter {
public:
    int fd_;
    bool is_error_ = false;
private:
    //是否正在关闭
    bool is_closing_ = false;
    std::list<std::shared_ptr<WriteObject>> objs_;  //双向链表
public:
    inline void SetFd(int fd){
        fd_ = fd;
    }
    inline int GetFd(){
        return fd_;
    }

    void EntireWrite(std::shared_ptr<char> buff, std::streamsize len);
    void LingerClose(); //全部发完完再关闭
    void OnWriteable();
private:
    void EntireWriteWhenEmpty(std::shared_ptr<char> buff, std::streamsize len);
    void EntireWriteWhenNotEmpty(std::shared_ptr<char> buff, std::streamsize len);
    bool WriteFrontObj();
    bool HandleError();
};

#endif  // __CONN_WRITER_H__